#include <windows.h>
#include <wininet.h>

void CALLBACK InternetStatusCallback(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength);

#define URL TEXT("https://www.baidu.com/")

void mainCRTStartup()
{
    HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    HINTERNET hOpen = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
    InternetSetStatusCallback(hOpen, InternetStatusCallback);
    InternetOpenUrl(hOpen, URL, NULL, 0, 0, (DWORD_PTR)hEvent);
    WaitForSingleObject(hEvent, INFINITE);
    InternetCloseHandle(hOpen);
    CloseHandle(hEvent);
    ExitProcess(0);
}

void CALLBACK InternetStatusCallback(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
{
    UNREFERENCED_PARAMETER(hInternet);
    UNREFERENCED_PARAMETER(dwInternetStatus);
    UNREFERENCED_PARAMETER(lpvStatusInformation);
    UNREFERENCED_PARAMETER(dwStatusInformationLength);
    if (dwInternetStatus != INTERNET_STATUS_REQUEST_COMPLETE)
        return;

    HINTERNET hUrl = *(HINTERNET *)lpvStatusInformation;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD dwRead = 0;
    do
    {
        char buf[0x100];
        InternetReadFile(hUrl, buf, sizeof buf, &dwRead);
        WriteConsoleA(hStdOut, buf, dwRead, NULL, NULL);
    } while (dwRead > 0);
    InternetCloseHandle(hUrl);
    SetEvent((HANDLE)dwContext);
}